package com.sr.examination.news;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;

import com.sr.examination.net.Net;
import com.tl.commonlibrary.network.ErrorResponse;
import com.tl.commonlibrary.network.RequestListener;
import com.tl.commonlibrary.network.bean.base.BaseBean;
import com.tl.commonlibrary.ui.beans.NewsBean;
import com.tl.commonlibrary.ui.web.WebActivity;
import com.tl.commonlibrary.ui.web.WebModel;

import retrofit2.Call;

/**
 * Created by JongSung on 18/3/14.
 */

public class NewsDetailActivity extends WebActivity {
    public static void start(Context context, NewsBean newsBean) {
        Intent intent = newIntent(context, "", WebModel.FROM_TYPE_DEFAULT);
        intent.setClass(context, NewsDetailActivity.class);
        intent.putExtra("newsBean", newsBean);
        context.startActivity(intent);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        NewsBean newsBean = (NewsBean) getIntent().getSerializableExtra("newsBean");
        if (newsBean == null) {
            finish();
            return;
        }
        setTitle(newsBean.getTitle());
        getNewsDetail(newsBean.getId());
    }

    private void getNewsDetail(int id) {
        showLoading();
        Net.getNewsDetail(id, new RequestListener<BaseBean<NewsBean>>() {
            @Override
            public void onSuccess(Call<BaseBean<NewsBean>> call, BaseBean<NewsBean> response) {
                if (response.isSuccessful()) {
                    setTitle(response.data.getTitle());
                    loadHtml(response.data.getContent());
                }
                dismissAll();
            }

            @Override
            public void onFailed(Call<BaseBean<NewsBean>> call, ErrorResponse errorResponse) {
                dismissAll();
            }
        });
    }
}
